package com.bs.sc.base.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class StringUtil {

	/***
	 * 生成订单号，当前时间+随机6位数
	 * 
	 * @return
	 */
	public static String generateOrderId() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String d = sdf.format(date);
		d += randomNumber();
		return d;
	}

	/**
	 * 获取随机6位数
	 * 
	 * @return
	 */
	public static String randomNumber() {
		StringBuilder str = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			str.append(random.nextInt(10));
		}
		return str.toString();
	}

	public static String toJsonStr(Object obj) {
		ObjectMapper M = new ObjectMapper();
		String json = "";
		try {
			json = M.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
}
